package com.dzl.moive.service.impl;

import com.dzl.asynthread.SyncInitDataServiceImpl;
import com.dzl.base.service.BaseServiceImpl;
import com.dzl.constant.CommonConstants;
import com.dzl.moive.dao.WelfareMovieCityMapper;
import com.dzl.moive.pojo.WelfareMovieCity;
import com.dzl.moive.pojo.query.WelfareMovieCityQuery;
import com.dzl.moive.service.WelfareMovieCityService;
import com.dzl.moive.utils.MovieUtils;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电影城市(welfare_movie_city)表服务实现类
 * @author liuguanjun
 * @since 2021-11-02 11:12:42
 */
@Service("welfareMovieCityService")
public class WelfareMovieCityServiceImpl extends BaseServiceImpl<WelfareMovieCity> implements WelfareMovieCityService {

    @Autowired
    private WelfareMovieCityMapper mapper;
    @Autowired
    private SyncInitDataServiceImpl syncInitDataServiceImpl;

    @Override
    public List<WelfareMovieCity> getCityList(String cityName,Integer isupdate) {
        //List<WelfareMovieCitySortVo> list = new ArrayList<>();
        WelfareMovieCityQuery query = new WelfareMovieCityQuery();
        if(EmptyUtils.isNotEmpty(cityName)){
            query.setCityName(cityName);
        }
        if(isupdate!=null){
            query.setIsupdate(isupdate);
        }
        query.setParentcode("0");
        List<WelfareMovieCity> citylist = mapper.selectMovieCityList(query);
        if(EmptyUtils.isEmpty(citylist) && EmptyUtils.isEmpty(cityName) && isupdate==null){
            citylist = MovieUtils.getMovieCityList();
            if(EmptyUtils.isNotEmpty(citylist)){
                for (WelfareMovieCity city:citylist) {
                    city.setParentcode("0");
                    city.setIsupdate(CommonConstants.NO);
                }
                mapper.insertList(citylist);
                syncInitDataServiceImpl.addCountyList(citylist);
            }
        }
        /*if (EmptyUtils.isNotEmpty(citylist)) {
            Map<String, ArrayList<WelfareMovieCity>> cityMap = new HashMap<String, ArrayList<WelfareMovieCity>>();
            for (WelfareMovieCity city:citylist) {
                ArrayList<WelfareMovieCity> cityList = cityMap.get(city.getPinyin().substring(0,1));
                if(EmptyUtils.isEmpty(cityList)){
                    cityList = new ArrayList<WelfareMovieCity>();
                }
                cityList.add(city);
                cityMap.put(city.getPinyin().substring(0,1), cityList);
            }
            if(cityMap.size()>0) {
                for (Map.Entry<String, ArrayList<WelfareMovieCity>> entry : cityMap.entrySet()) {
                    WelfareMovieCitySortVo welfareMovieCitySortVo = new WelfareMovieCitySortVo();
                    welfareMovieCitySortVo.setCitycode(entry.getKey());
                    welfareMovieCitySortVo.setCitylist(entry.getValue());
                    list.add(welfareMovieCitySortVo);
                }
            }
        }*/
        return citylist;
    }

    @Override
    public List<WelfareMovieCity> getCountyList(String citycode) {
        //List<WelfareMovieCitySortVo> list = new ArrayList<>();
        WelfareMovieCityQuery query = new WelfareMovieCityQuery();
        query.setParentcode(citycode);
        List<WelfareMovieCity> citylist = mapper.selectMovieCityList(query);
        if(EmptyUtils.isEmpty(citylist)){
            citylist = MovieUtils.getMovieCountyList(citycode);
            if(EmptyUtils.isNotEmpty(citylist)){
                for (WelfareMovieCity city:citylist) {
                    city.setParentcode(citycode);
                    city.setCitycode(city.getId().toString());
                }
                mapper.insertList(citylist);
            }
        }
        return citylist;
    }

    @Override
    public WelfareMovieCity getCityByCityName(String cityName) {
        //List<WelfareMovieCitySortVo> list = new ArrayList<>();
        WelfareMovieCityQuery query = new WelfareMovieCityQuery();
        query.setCityName(cityName.replace("市",""));
        query.setParentcode("0");
        List<WelfareMovieCity> citylist = mapper.selectMovieCityList(query);
        return EmptyUtils.isNotEmpty(citylist)?citylist.get(0):null;
    }
}